package com.z2k.todo.services.impl;

import com.z2k.todo.dao.TodoDAO;
import com.z2k.todo.entity.Todo;
import com.z2k.todo.entity.TodoClass;
import com.z2k.todo.page.TodoClassVO;
import com.z2k.todo.services.TodoService;
import com.z2k.todo.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class TodoServiceImpl implements TodoService {
    @Override
    public List<TodoClassVO> listTodoClasses(Integer parentid) {
        SqlSession sqlSession = MybatisUtil.getInstance().getSqlSessionFactory().openSession();
        TodoDAO dao = sqlSession.getMapper(TodoDAO.class);
        List<TodoClassVO> result = new ArrayList<>();
        try{
            List<TodoClass> root = dao.listTodoClassesByParentid(parentid);
            for (TodoClass tc : root){
                TodoClassVO todoClassVO = new TodoClassVO();
                todoClassVO.setTodoclass(tc);
                todoClassVO.setTodos(dao.listTodosByClassid(tc.getId()));
                todoClassVO.setTodoclassvolist(listTodoClasses(tc.getId()));
                result.add(todoClassVO);
            }
        } finally{
            sqlSession.close();
        }
        return result;
    }

    @Override
    public Boolean saveTodoClass(TodoClass todoClass) {
        SqlSession sqlSession = MybatisUtil.getInstance().getSqlSessionFactory().openSession();
        TodoDAO dao = sqlSession.getMapper(TodoDAO.class);
        int result = 0;
        try{
            result = dao.saveTodoClass(todoClass);
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
        return result>0;
    }

    @Override
    public Boolean saveTodoClass(String todoClassName) {
        return null;
    }

    @Override
    public Boolean saveTodo(Todo todo) {
        SqlSession sqlSession  = MybatisUtil.getInstance().getSqlSessionFactory().openSession();
        TodoDAO dao = sqlSession.getMapper(TodoDAO.class);
        int result = 0;
        try{
            result = dao.saveTodo(todo);
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
        return result>0;
    }

    @Override
    public Todo saveTodo(String title, Integer todoClassid) {
        return null;
    }

    @Override
    public Boolean updateTodo(Todo todo) {
        return null;
    }

    @Override
    public Boolean updateTodo(Integer todoid) {
        SqlSession sqlSession = MybatisUtil.getInstance().getSqlSessionFactory().openSession();
        TodoDAO dao = sqlSession.getMapper(TodoDAO.class);
        int result = 0;
        try{
            result = dao.updateTodo(todoid);
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
        return result>0;
    }

    @Override
    public Boolean deletetodo(Integer todoid) {
        SqlSession sqlSession = MybatisUtil.getInstance().getSqlSessionFactory().openSession();
        TodoDAO dao = sqlSession.getMapper(TodoDAO.class);
        int result = 0;
        try{
            result = dao.deleteTodo(todoid);
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
        return result>0;
    }
}
